package com.slliver.controller;

import com.slliver.pojo.Order;
import com.slliver.pojo.Product;
import com.slliver.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }


    @GetMapping("/test")
    public String test() {
        return "product-test";
    }

    @GetMapping("/get/{productId}")
    public Product get(@PathVariable("productId") Integer productId) {
        return productService.getProductById(productId);
    }

    @GetMapping("/getOrder/{orderId}")
    public Order getOrder(@PathVariable("orderId") Integer orderId) {
        return productService.getOrder(orderId);
    }


    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 获取某个服务的信息
     */
    @GetMapping("/getDiscovery")
    public DiscoveryClient getDiscovery() {
        List<String> services = discoveryClient.getServices();
        List<ServiceInstance>  serviceInstanceList = discoveryClient.getInstances("ORDER-SERVICE");
        for (ServiceInstance serviceInstance : serviceInstanceList) {
            System.out.println(serviceInstance.getInstanceId());
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getPort());
            System.out.println(serviceInstance.getUri().toString());
        }
        return discoveryClient;
    }
}
